package io.renren.modules.frt.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.mapper.EntityWrapper;

import io.renren.common.utils.MapUtils;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
import io.renren.modules.frt.entity.MonitorReport1Entity;
import io.renren.modules.frt.service.MonitorReport1Service;
import io.renren.modules.frt.service.TaskUserService;



/**
 * 表1：全国林业产业发展监测统计表
 *
 * @author qibing.huang
 * @email abingtech@163.com
 * @date 2018-11-25 10:25:32
 */
@RestController
@RequestMapping("frt/monitorreport1")
public class MonitorReport1Controller {
    @Autowired
    private MonitorReport1Service monitorReport1Service;
    @Autowired
    private TaskUserService taskUserService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("frt:monitorreport1:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = monitorReport1Service.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{reportId}")
    public R info(@PathVariable("reportId") Long reportId){
        List<MonitorReport1Entity> list = monitorReport1Service.selectList(new EntityWrapper<MonitorReport1Entity>().eq("report_id", reportId));

        return R.ok().put("list", list);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @Transactional
    public R save(@RequestBody List<MonitorReport1Entity> list){
    	if(list == null || list.size() == 0) {
    		return R.error("参数不合法");
    	}
    	
    	if(list.get(0).getTaskUserId() == null) {
    		return R.error("task_user_id["+list.get(0).getTaskUserId()+"]参数错误");
    	}
    	
		monitorReport1Service.insertBatch(list);
		
		taskUserService.onlineCallbackTaskUser(list.get(0).getTaskUserId(), list.get(0).getReportId());

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @Transactional
    public R update(@RequestBody List<MonitorReport1Entity> list){
    	if(list == null || list.size() == 0) {
    		return R.error("参数不合法");
    	}
    	if(list.get(0).getTaskUserId() == null) {
    		return R.error("task_user_id["+list.get(0).getTaskUserId()+"]参数错误");
    	}
    	if(list.get(0).getReportId() == null) {
    		return R.error("report_id["+list.get(0).getReportId()+"]参数错误");
    	}
    	
    	monitorReport1Service.deleteByMap(new MapUtils().put("report_id", list.get(0).getReportId()));
    	
    	monitorReport1Service.insertBatch(list);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("frt:monitorreport1:delete")
    public R delete(@RequestBody Long[] reportIds){
		monitorReport1Service.deleteBatchIds(Arrays.asList(reportIds));

        return R.ok();
    }

}
